package com.proxy.autoclick.checker.http.proxy;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;

import com.proxy.autoclick.clicker.valueobject.KeywordAddress;
import com.proxy.autoclick.common.util.Constants;
import com.proxy.autoclick.common.util.RandomNumUtil;
import com.proxy.autoclick.common.valueobject.Proxy;
/**
 * Handle checking google host class.
 *
 */
public class ClientExecuteProxySearchGoogleHost extends ClientExecuteProxy{
	
	private KeywordAddress keywordAddress;
	
	public ClientExecuteProxySearchGoogleHost(HttpClient httpclient,Proxy myProxy,KeywordAddress keywordAddress, int pageSize) throws URISyntaxException {
		super(httpclient,myProxy, getHttpGet(keywordAddress,pageSize));
		this.keywordAddress = keywordAddress;
	}
	
	private static HttpGet getHttpGet(KeywordAddress keywordAddress, int pageSize) throws URISyntaxException
	{
		URIBuilder builder = new URIBuilder();
//		builder.setScheme("http").setHost("ajax.googleapis.com").setPath("/ajax/services/search/web")
		builder.setScheme("http").setHost(Constants.GOOGLE_HOST_NAME).setPath("/search")
		    .setParameter("q", keywordAddress.getKeyword())
		    .setParameter("btnG", keywordAddress.getKeyword())
		    .setParameter("aq", "f")
		    .setParameter("oq", "")
		    .setParameter("start", String.valueOf(pageSize))
//		    .setParameter("v", "1.0")
//		    .setParameter("hl", "zh-CN")
//		    .setParameter("source", "hp")
//		    .setParameter("aqi", "")
//		    .setParameter("aql", "")
//		    .setParameter("gs_rfai", "")
		    ;
		URI uri = builder.build();
		HttpGet httpGet = new HttpGet(uri);
		return httpGet;
	}


	/**
	 * get valid display string for valid google proxy.
	 */
	public String getValidDisplayString() {
		return " Send query for " + this.keywordAddress.getKeyword()  + " to Google (sucess)";
	}

	/**
	 * get invalid display string for invalid google proxy.
	 */
	public String getInvalidDisplayString() {
		return " Send query for " + this.keywordAddress.getKeyword()  + " to Google (failed)";
	}


	/**
	 * update validForGoogleHost to true for myProxy object
	 * set the update time in myProxy object.
	 */
	public void updateResultByValid(Proxy myProxy) {
		myProxy.setValidForGoogleHost(true);
		myProxy.setUpdateTimeForGoogleHost(new Date());
	}


	/**
	 * update validForGoogleHost 
	 * to false for myProxy object
	 */
	public void updateResultByInvalid(Proxy myProxy) {
		myProxy.setValidForGoogleHost(false);
	}


	public String[] getRegexString() {
		return Constants.GOOGLE_REGEX_STRING;
	}

	public int getVisitPageDelay() {
		return RandomNumUtil.getNumBetweenRange(mainFrame.getPageDelayFrom(), 
					mainFrame.getPageDelayTo())*1000;
	}

	public void setValidOutputMessage(String message) {
		mainFrame.setTextAreaText(message);
	}

	public void setInValidOutputMessage(String message) {
		mainFrame.setTextAreaText(message);
	}

}
